<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ajax-01-原生方式</title>
</head>
<style>
    * {
        box-sizing: border-box;
    }

    body {
        background-color: #222;
        color: snow;
    }
</style>
<body>
<button onclick="getData()">Ajax从服务器异步获取数据</button>
<div id="rps-msg">服务器响应GET请求的数据将显示在这里</div>
<script>
    function getData() {
        //1.创建XMLHttpRequest对象
        let xmlHttpRequest = new XMLHttpRequest();
        //2.发送异步请求
        xmlHttpRequest.open('GET', 'https://jsonplaceholder.typicode.com/posts');
        xmlHttpRequest.send();
        //3.获取服务响应数据
        xmlHttpRequest.onreadystatechange = function () {
            if (xmlHttpRequest.readyState === 4 && xmlHttpRequest.status === 200) {
                document.getElementById('rps-msg').innerHTML = xmlHttpRequest.responseText;
            }
        };
    }
</script>
</body>
</html>